/*
 *************************************************************************
 *西瑞克斯研发管理系统
 *版 本: 1.0   
 *日 期：02/06/2012    
 *作 者： 王浩 QQ：873987060   
 *************************************************************************   
 */
package com.application.yfprojectsurvey.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.application.department.entity.Department;
import com.application.usertable.entity.Usertable;
import com.application.yfproject.entity.YfProject;
import com.application.yfprojectsurvey.entity.YfProjectSurvey;
import com.application.yfprojectsurvey.struts.form.YfProjectSurveyForm;
import com.manager.BaseDao;
import com.manager.commons.page.PageBean;

/**
 * 
 * Description:YfProjectSurveyDAO<br />
 * 
 * Version:V0.1<br />
 * 
 * Author: 王浩 QQ：873987060 <br />
 * 
 * History: <br />
 * 
 * 02/06/2012
 * 
 */
public class YfProjectSurveyDAO extends BaseDao {
	private static final Logger logger = Logger
			.getLogger(YfProjectSurveyDAO.class);

	private YfProjectSurvey yfProjectSurvey;

	public YfProjectSurvey getYfProjectSurvey() {
		return this.yfProjectSurvey;
	}

	public void setYfProjectSurvey(YfProjectSurvey yfProjectSurvey) {
		this.yfProjectSurvey = yfProjectSurvey;
	}

	public Integer getAllCountYfProjectSurvey(
			YfProjectSurveyForm yfProjectSurveyForm) {
		// 储存查询条件的buffer
		StringBuffer buf = new StringBuffer();
		buf.append("select count(*) from YfProjectSurvey t,YfProject p,Usertable u ,Department d where t.projectNo = p.projectNo and t.operateid = u.id and p.projectOrg=d.id");
		// 查询字符串
		String strSql = getStringBuffer(yfProjectSurveyForm, buf).toString();

		return Integer.parseInt(this.getHibernateTemplate().find(strSql).get(0)
				.toString());
	}

	public void deleteYfProjectSurveys(YfProjectSurveyForm yfProjectSurveyForm) {
		String hql = " delete from YfProjectSurvey where id in  ( '" + yfProjectSurveyForm.getIds() + "')";
		Session session = this.getSession();
		try {
			session.createQuery(hql).executeUpdate();
		} catch (RuntimeException re) {
			logger.error("deleteYfProjectSurveys failed");
			throw re;
		} finally {
			if (session != null) {
				releaseSession(session);
			}
		}
	}

	public List searchYfProjectSurvey(YfProjectSurveyForm yfProjectSurveyForm,final PageBean pageBean) {
		StringBuffer buf = new StringBuffer();

		// 返回list集合
		List list = null;

		buf.append(" from YfProjectSurvey t,YfProject p,Usertable u,Department d   where t.projectNo = p.projectNo and t.operateid = u.id and p.projectOrg=d.id ");

		// 查询字符串条件
		buf = getStringBuffer(yfProjectSurveyForm, buf);

		// 传递查询条件buf值
		pageBean.setStringBuffer(buf);

		// 分页取得字符串集合
		list = getHibernateTemplate().executeFind(new HibernateCallback() {

			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {

				Query query = session.createQuery(pageBean.getStringBuffer().toString()+ " order by t.createDate Desc ");

				// 设置开始页
				query.setFirstResult(pageBean.getPageSize()
						* (pageBean.getPageIndex() - 1));

				// 设置结束页
				query.setMaxResults(pageBean.getPageSize());

				List resutlList = new ArrayList();
				List teamsList = query.list();
				if (teamsList != null && teamsList.size() > 0) {
					for (int j = 0; j < teamsList.size(); j++) {
						Object[] objs = (Object[]) teamsList.get(j);
						YfProjectSurvey yfProjectSurvey = (YfProjectSurvey) objs[0];
						YfProject yfProject = (YfProject) objs[1];
						Usertable user = (Usertable) objs[2];
						Department depart = (Department) objs[3];
						yfProjectSurvey.setProjectName(yfProject.getProjectName());
						yfProjectSurvey.setProjectOrg(yfProject.getProjectOrg());
						yfProjectSurvey.setRname(user.getRname());
						yfProjectSurvey.setProjectOrgName(depart.getName());
						resutlList.add(yfProjectSurvey);
					}
				}
				return resutlList;
			}
		});
		return list;
	}
	
	public List searchYfProjectSurvey(YfProjectSurveyForm yfProjectSurveyForm) {
		StringBuffer buf = new StringBuffer();

		// 返回list集合
		List list = null;
  
		buf.append("from YfProjectSurvey t,YfProject p ,Usertable u,Department d     where t.projectNo = p.projectNo and t.operateid = u.id and p.projectOrg=d.id ");

		// 查询字符串条件
		buf = getStringBuffer(yfProjectSurveyForm, buf);

        final String sql = buf.toString();

		// 分页取得字符串集合
		list = getHibernateTemplate().executeFind(new HibernateCallback() {

			public Object doInHibernate(Session session) throws HibernateException, SQLException {

				Query query = session.createQuery(sql+ " order by t.createDate Desc ");

				List resutlList = new ArrayList();
				List teamsList = query.list();
				if (teamsList != null && teamsList.size() > 0) {
					for (int j = 0; j < teamsList.size(); j++) {
						Object[] objs = (Object[]) teamsList.get(j);
						YfProjectSurvey yfProjectSurvey = (YfProjectSurvey) objs[0];
						YfProject yfProject = (YfProject) objs[1];
						Usertable user = (Usertable) objs[2];
						Department depart = (Department) objs[3];
						yfProjectSurvey.setProjectName(yfProject.getProjectName());
						yfProjectSurvey.setProjectOrg(yfProject.getProjectOrg());
						yfProjectSurvey.setRname(user.getRname());
						yfProjectSurvey.setProjectOrgName(depart.getName());
						resutlList.add(yfProjectSurvey);
					}
				}
				return resutlList;
			}
		});
		return list;
	}

	/**
	 * 查询条件的封装
	 * 
	 * @param departmentForm
	 * @return
	 */
	private StringBuffer getStringBuffer(
			YfProjectSurveyForm yfProjectSurveyForm, StringBuffer buf) {
		/*
		 * 需要加入查询条件时封装
		 */

		if (StringUtils.isNotBlank(yfProjectSurveyForm.getDocumentName())) {
			buf.append(" and t.documentName like'%" + yfProjectSurveyForm.getDocumentName().trim() + "%'");
		}
		if (StringUtils.isNotBlank(yfProjectSurveyForm.getProjectNo())) {
			buf.append(" and t.projectNo = '" + yfProjectSurveyForm.getProjectNo().trim() + "'");
		}
		if (StringUtils.isNotBlank(yfProjectSurveyForm.getDocumentType())) {
			buf.append(" and t.documentType like'%" + yfProjectSurveyForm.getDocumentType().trim() + "%'");
		}

		if (StringUtils.isNotBlank(yfProjectSurveyForm.getOperateid())) {
			buf.append(" and t.operateid = '" + yfProjectSurveyForm.getOperateid()+"'");
		}
		if (StringUtils.isNotBlank(yfProjectSurveyForm.getProjectName())) {
			buf.append(" and p.projectName like'" + yfProjectSurveyForm.getProjectName().trim() + "%'");
		}
		if (StringUtils.isNotBlank(yfProjectSurveyForm.getProjectOrg())) {
			buf.append(" and p.projectOrg = '" + yfProjectSurveyForm.getProjectOrg().trim() + "'");
		}
		if (StringUtils.isNotBlank(yfProjectSurveyForm.getId())) {
			buf.append(" and t.id = '" + yfProjectSurveyForm.getId()+"'");
		}
		return buf;
	}
}
